//
//  OrderAdditionController.m
//  StarOrangeStaff
//
//  Created by lshenrong on 2020/7/20.
//  Copyright © 2020 lshenrong. All rights reserved.
//

#import "OrderAdditionController.h"
#import "TempletThirteenTableViewCell.h"
#import "InputTempletTwelveTableViewCell.h"
#import "OrderTransportWayUseCell.h"
#import <BRPickerView/BRPickerView.h>
#import "FSActionSheet.h"



@interface OrderAdditionController()<UITableViewDataSource,UITableViewDelegate,FSActionSheetDelegate >

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) CedarButton *comfirmBtn;
@property (nonatomic,strong) NSString *carUseType;


@end

@implementation OrderAdditionController

 -(void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新增订单";
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(self.view);
    }];
}

-(void)makeDatePicker {
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]init];
    datePickerView.pickerMode = BRDatePickerModeYMDHM;
    datePickerView.title = @"选择开始时间";
    datePickerView.selectDate = [NSDate br_setYear:2020 month:06 day:30];
    datePickerView.minDate = [NSDate br_setYear:1949 month:3 day:12];
    datePickerView.maxDate = [NSDate br_setYear:2022 month:06 day:30];
    datePickerView.isAutoSelect = YES;
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        NSLog(@"选择的值：%@", selectValue);
    };
    [datePickerView show];

}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.00f;
}



//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = kAppBackgoundColor;
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     TempletThirteenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TempletThirteenTableViewCellIdentifier"];
     OrderTransportWayUseCell *firstCell = [tableView dequeueReusableCellWithIdentifier:@"OrderTransportWayUseCellIdentifier"];
     InputTempletTwelveTableViewCell *secondCell = [tableView dequeueReusableCellWithIdentifier:@"InputTempletTwelveTableViewCellIdentifier"];
    
    
    if (indexPath.section == 2) {
         if (indexPath.row == 0) {
             cell.btnPlaceHolder = @"请输入公司名称 ";
             [cell updateTitle:@"公司名"];
             return cell;
           }else if (indexPath.row == 1) {
               secondCell.placeholder= @"请订车人姓名";
               [secondCell setCellTitle:@"订车人"];

           }else if (indexPath.row == 2) {
               secondCell.placeholder = @"请输入订车人号码";
               [secondCell setCellTitle:@"订车人号码"];

               
           }else if (indexPath.row == 3) {
               secondCell.placeholder= @"乘车人姓名";
               [secondCell setCellTitle:@"乘车人"];

           }else if (indexPath.row == 4) {
               secondCell.placeholder = @"请输入乘车人号码";
               [secondCell setCellTitle:@"乘车人号码"];
           }
           return secondCell;

    }else if (indexPath.section == 1 ) {
        @weakify(self)
        if (indexPath.row == 0) {
            cell.btnPlaceHolder = @"请选择车型级别";
           [cell updateTitle:@"用车等级"];
            cell.chooseBtn.clickEvent = ^{
            @strongify(self)
                [self selectcCarUseType:indexPath];
            };
        }else if (indexPath.row == 1) {
            cell.btnPlaceHolder = @"选择品牌";
            [cell updateTitle:@"品牌"];
            cell.chooseBtn.clickEvent = ^{
            @strongify(self)
                [self selectcCarBrandType:indexPath];
            };

            
        }else if (indexPath.row == 2) {
            cell.btnPlaceHolder = @"请选择时间";
            [cell updateTitle:@"用车时间"];
             cell.chooseBtn.clickEvent = ^{
                 @strongify(self)
                [self makeDatePicker];
            };
        }
        else if (indexPath.row == 3) {
            cell.btnPlaceHolder = @"选择结束时间";
            [cell updateTitle:@"预计结束时间"];
            cell.chooseBtn.clickEvent = ^{
                 @strongify(self)
                [self makeDatePicker];
            };
        }else if (indexPath.row == 4) {
            secondCell.placeholder= @"输入出发地";
            [secondCell setCellTitle:@"出发地"];
            return secondCell;
            
        }else if (indexPath.row == 5) {
            secondCell.placeholder= @"输入目的地";
            [secondCell setCellTitle:@"目的地"];
            return secondCell;
           
        }else if (indexPath.row == 6) {
            cell.btnPlaceHolder = @"选择用车数量";
            [cell updateTitle:@"用车数量"];
           
        }else if (indexPath.row == 7) {
            cell.btnPlaceHolder = @"选择座位数";
            [cell updateTitle:@"座位数"];
           
        }else if (indexPath.row == 8) {
            cell.btnPlaceHolder = @"选择订车天数";
            [cell updateTitle:@"订车天数"];
            
        }
    }else if (indexPath.section == 0) {
        return firstCell;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
        }
    }
}


 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return   3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 2) {
        return 5;
    }else  if (section == 1){
        return 9;
    }else {
        return 1;
    }
}

- (void)selectcCarBrandType:(NSIndexPath *)indexPath {
    NSMutableArray *actionSheetItems = [@[FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"经济型"),
                                             FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"丰田"),
                                             FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"本田"),FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"比亚迪")]
                                           mutableCopy];
       FSActionSheet *actionSheet = [[FSActionSheet alloc] initWithTitle:nil cancelTitle:nil items:actionSheetItems];
       // 展示并绑定选择回调
       [actionSheet showWithSelectedCompletion:^(NSInteger selectedIndex) {
           FSActionSheetItem *item = actionSheetItems[selectedIndex];
           self.carUseType = item.title;
           [self.tableView reloadData];
       }];
    
}
    

- (void)selectcCarUseType:(NSIndexPath *)indexPath {
    
    NSMutableArray *actionSheetItems = [@[FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"经济型"),
                                          FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"舒适型"),
                                          FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"商务型"),FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"豪华型"),FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"巴车"),FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"超豪华"),FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"双牌照"),]
                                        mutableCopy];
    FSActionSheet *actionSheet = [[FSActionSheet alloc] initWithTitle:nil cancelTitle:nil items:actionSheetItems];
    // 展示并绑定选择回调
    [actionSheet showWithSelectedCompletion:^(NSInteger selectedIndex) {
        FSActionSheetItem *item = actionSheetItems[selectedIndex];
        self.carUseType = item.title;
        [self.tableView reloadData];
    }];
    
}

- (void)FSActionSheet:(FSActionSheet *)actionSheet selectedIndex:(NSInteger)selectedIndex {
    
    
}

- (CedarButton *)comfirmBtn{
    if (!_comfirmBtn){
        _comfirmBtn =  [CedarButton buttonWithTextForGradientForCommomn:@"确认" radius:YES];
        @weakify(self)
        _comfirmBtn.clickEvent = ^{
            @strongify(self)
        };
    }
    return _comfirmBtn;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [UIConfigure tableViewFromFactory];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 50;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
        [self setUpFooterView];
        [_tableView registerClass:[TempletThirteenTableViewCell class] forCellReuseIdentifier:@"TempletThirteenTableViewCellIdentifier"];
        [_tableView registerClass:[InputTempletTwelveTableViewCell class] forCellReuseIdentifier:@"InputTempletTwelveTableViewCellIdentifier"];
        [_tableView registerClass:[OrderTransportWayUseCell class] forCellReuseIdentifier:@"OrderTransportWayUseCellIdentifier"];

    }
    return _tableView;
}

- (void)setUpFooterView{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
   [footerView addSubview:self.comfirmBtn];
   [self.comfirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.bottom.mas_equalTo(footerView.mas_bottom).mas_offset(-40);
       make.left.mas_equalTo(footerView).mas_offset(kMarginOffsetX);
       make.right.mas_equalTo(footerView).mas_offset(-kMarginOffsetX);
       make.height.mas_offset(40);
   }];
   footerView.backgroundColor = kAppBackgoundColor;
   _tableView.tableFooterView = footerView;
}


@end
